<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串操作</title>
</head>

<body>
    <script>
        //1.去掉字符串中的空格
        //position:left|right|middle|all|both
        /* function trimStr(str, position) {
            const regObj = {
                left: /^\s+/,
                right: /\s+$/,
                middle: /[\s]+/g,
                both: /(^\s+)|(\s+$)/g,
                all: /\s+/g,
            }
            if (!str) {
                return
            }
            const reg = position && regObj[position] ? regObj[position] : regObj.all
            let newStr = ''
            if (position == 'middle') {
                let match = str.match(regObj[position])
                let rleft = match[0]
                let rright = match[match.length - 1]
                newStr = str.replace(reg, '')
                newStr = rleft + newStr + rright
            } else {
                newStr = str.replace(reg, '')
            }
            return newStr
        }
        //str.split(' ').join('')
        console.log(trimStr('  ab  b c ff   ', 'middle'))
        console.log(trimStr('    ab   ', 'left'))
        console.log(trimStr('    ab   ', 'right'))
        console.log(trimStr('    ab   ', 'both'))
        console.log(trimStr(' ab c  dd e   ', 'all')) */
        /*去掉其他字符：
         * \f  匹配换页字符。
         * \n  匹配换行字符。
         * \r  匹配回车符字符。
         * \t  匹配制表字符。
         * \v  匹配垂直制表符。
         * /[\f\n\r\t\v]/g
         */


        //2.去除字符串中最后一个指定字符
        /*   function trimChar(str, target) {
              if (!str) {
                  return
              }
              return str.split('').reverse().join('').replace(target, '').split('').reverse().join('')
              const idx = str.lastIndexOf(target);
              if (idx > -1) {
                  return str.slice(0, idx) + str.slice(idx + 1);
              } else {
                  return str;
              }
          }
          console.log(trimChar('abaabaabaaba^p^abaa', '^')) */

        //3.切换大小写
        /*        function changeStr(str) {
                   let strArr = str.split('')
                   for (let i in strArr) {
                       let x = strArr[i]
                       if (x === x.toUpperCase()) {
                           strArr[i] = x.toLowerCase()
                       } else if (x === x.toLowerCase()) {
                           strArr[i] = x.toUpperCase()
                       }

                   }
                   return strArr.join('')
               }
               let str = 'abaAbaaBa'
               console.log("转换前:", str, "转换后:", changeStr(str)) */
        //4.命名转换
        /*  function CamelToUnderScore(str) {
        str = str[0].toLowerCase() + str.slice(1);
        let result = str;
        (str.match(/[A-Z]/g) || []).forEach(w => {
            result = result.replace(w, `_${w.toLowerCase()}`)

        });
        return result
        }
        console.log(CamelToUnderScore("aBcDeFg"));
        console.log(CamelToUnderScore("ABCDEFG")); //_a...实际上应该是a_...把 */

        /* function UnderScoreToCamel(str) {
            let strArr = str.split('_')
            for (let i in strArr) {
                if (i != 0)
                    strArr[i] = strArr[i].substr(0, 1).toUpperCase() + strArr[i].substr(1)
            }
            return strArr.join('')
        }
        console.log(UnderScoreToCamel('user_name_space_name_abaaba')) */
        //计算子串在父串中出现的次数
        function strCount(str, parentStr) {
            if (!parentStr || !str) return 0
            //1.正则
            /* let reg = new RegExp(str, 'g')
            return parentStr.match(reg).length */
            /*正向断言RegExp(`(?=${str})`,'g')*/
            //2.split
            /* return parentStr.split(str).length - 1 */
            //3.includes
            let count = 0
            while (parentStr.includes(str)) {
                parentStr = parentStr.replace(str, "")
                count++
            }
            return count
        }
        console.log(strCount('aa', "aaabcaaa"))
    </script>
</body>

</html>